package com.lanhe.admin.controller.memberBase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lanhe.common.annotation.Log;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.common.core.domain.entity.LhMemberBase;
import com.lanhe.common.core.domain.entity.vo.LhMemberStatisticsVo;
import com.lanhe.common.core.page.TableDataInfo;
import com.lanhe.common.enums.BusinessType;
import com.lanhe.common.utils.poi.ExcelUtil;
import com.lanhe.member.domain.dto.BindingMemberBaseDTO;
import com.lanhe.member.service.ILhMemberBaseService;
import com.lanhe.teacher.domain.vo.SelectTeacherVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 会员基础Controller
 * 
 * @author liang
 * @date 2024-01-06
 */
@RestController
@RequestMapping("/member/memberBase")
public class LhMemberBaseController extends BaseController
{
    @Autowired
    private ILhMemberBaseService lhMemberBaseService;

    /**
     * 查询会员基础列表
     */
    @PreAuthorize("@ss.hasPermi('member:memberBase:list')")
    @GetMapping("/list")
    public TableDataInfo list(LhMemberBase lhMemberBase)
    {
        startPage();
        List<LhMemberBase> list = lhMemberBaseService.selectLhMemberBaseList(lhMemberBase);
        return getDataTable(list);
    }

    @GetMapping("/recemMemberList")
    public List<SelectTeacherVo> recemMemberList(LhMemberBase lhMemberBase)
    {
        List<SelectTeacherVo> teacherVoList = new ArrayList<>();
        List<LhMemberBase> list = lhMemberBaseService.selectLhMemberBaseList(lhMemberBase);
        list.forEach(item ->{
            SelectTeacherVo selectTeacherVo = new SelectTeacherVo();
            selectTeacherVo.setValue(item.getUserName());
            selectTeacherVo.setAddress(item.getMemberId().toString());
            selectTeacherVo.setRegisterNo(item.getRegisterNo().toString());
            teacherVoList.add(selectTeacherVo);
        });
        return teacherVoList;
    }

    /**
     * 获取会员基础详细信息
     */
    @PreAuthorize("@ss.hasPermi('member:memberBase:query')")
    @GetMapping(value = "/{memberId}")
    public AjaxResult getInfo(@PathVariable("memberId") Long memberId)
    {
        LambdaQueryWrapper<LhMemberBase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LhMemberBase::getMemberId,memberId);
        return success(lhMemberBaseService.getOne(lambdaQueryWrapper));
    }


    @PreAuthorize("@ss.hasPermi('member:memberBase:edit')")
    @Log(title = "修改会员基础", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult audit(@Validated @RequestBody LhMemberBase lhMemberBase)
    {
        if (ObjectUtils.isEmpty(lhMemberBase.getMemberId())
                || null == lhMemberBase.getMemberId()) {
            return error("主键ID不能为空");
        }


        System.err.println("lhMemberBase  "+lhMemberBase);
        LambdaUpdateWrapper<LhMemberBase> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(LhMemberBase::getMemberId,lhMemberBase.getMemberId());

        return toAjax(lhMemberBaseService.update(lhMemberBase,updateWrapper));
//        lhMemberBase.setUpdateTime(new Date());
//        LambdaQueryWrapper<LhMemberBase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(LhMemberBase::getDelFlag,0)
//                .eq(LhMemberBase::getMemberId,lhMemberBase.getMemberId());
//        return toAjax(lhMemberBaseService.update(lhMemberBase,lambdaQueryWrapper));
    }


    @Log(title = "绑定修改", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/bindingMemberBase")
    public AjaxResult auditBindingMemberBase(@Validated @RequestBody BindingMemberBaseDTO bindingMemberBaseDTO)
    {
        System.err.println("BindingMemberBaseDTO  "+bindingMemberBaseDTO);
        if (ObjectUtils.isEmpty(bindingMemberBaseDTO.getStudentIds())) {
            return error("学员主键ID不能为空");
        }
        return toAjax(lhMemberBaseService.bindingMemberBase(bindingMemberBaseDTO));
    }


    @GetMapping("/getMemberStat")
    public LhMemberStatisticsVo getMemberStat()
    {
        return lhMemberBaseService.getMemberStat();
    }

    /**
     * 导出会员基础列表
     */
//    @PreAuthorize("@ss.hasPermi('member:memberBase:export')")
    @Log(title = "会员基础", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LhMemberBase lhMemberBase)
    {
        System.err.println("lhMemberBase查询内容 "+ lhMemberBase);
        List<LhMemberBase> list = lhMemberBaseService.selectLhMemberBaseList(lhMemberBase);
        System.err.println("list "+list);
        ExcelUtil<LhMemberBase> util = new ExcelUtil<LhMemberBase>(LhMemberBase.class);
        util.exportExcel(response, list, "会员基础数据");
    }



}
